/*******************************************************************************
 * Copyright (c) 2009 Dop Sun.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Dop Sun - initial API and implementation
 ******************************************************************************/
package com.softao.ming.gde.internal.ui.launcher;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.jdt.ui.ISharedImages;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

/**
 * 
 */
public class GdeApplicationMainPage extends GwtLaunchTab {
	private final ITabBlock mWorkspaceData;
	private final ITabBlock mProgramBlock;
	private final ITabBlock mConfigurationBlock;
	
	/**
	 * 
	 */
	public GdeApplicationMainPage() {
		mWorkspaceData = createWorkspaceDataBlock();
		mProgramBlock = createProgramBlock();
		mConfigurationBlock = createConfigurationBlock();
	}
	
	@Override
	public void createControl(Composite parent) {
		final ScrolledComposite scrollContainer = new ScrolledComposite(parent, SWT.V_SCROLL);
		scrollContainer.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		Composite composite = new Composite(scrollContainer, SWT.NONE);
		scrollContainer.setContent(composite);
		composite.setLayout(new GridLayout());
		composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		mWorkspaceData.createControl(composite);
		mProgramBlock.createControl(composite);
		mConfigurationBlock.createControl(composite);
		
		Dialog.applyDialogFont(composite);
		composite.setSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		scrollContainer.setExpandHorizontal(true);
		this.setControl(scrollContainer);
	}

	@Override
	public String getName() {
		return "Main";
	}
	
	@Override
	public Image getImage() {
		return JavaUI.getSharedImages().getImage(ISharedImages.IMG_OBJS_CLASS);
	}

	@Override
	public void initializeFrom(ILaunchConfiguration configuration) {
		try {
			mWorkspaceData.initializeFrom(configuration);
			mProgramBlock.initializeFrom(configuration);
			mConfigurationBlock.initializeFrom(configuration);
		} catch (CoreException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void performApply(ILaunchConfigurationWorkingCopy configuration) {
		mWorkspaceData.performApply(configuration);
		mProgramBlock.performApply(configuration);
		mConfigurationBlock.performApply(configuration);
	}

	@Override
	public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
		mWorkspaceData.setDefaults(configuration);
		mProgramBlock.setDefaults(configuration);
		mConfigurationBlock.setDefaults(configuration);
	}

	private WorkspaceDataBlock createWorkspaceDataBlock() {
		WorkspaceDataBlock result = new WorkspaceDataBlock(this);
		return result;
	}
	
	private ITabBlock createProgramBlock() {
		ProgramBlock result = new ProgramBlock(this);
		return result;
	}
	
	private ITabBlock createConfigurationBlock() {
		return new ConfigurationDataBlock(this);
	}
	
	@Override
	public String getId() {
		return "com.softao.gde.ui.launch.tab.main";
	}
	
}
